#include "includes.h"

extern int state;
extern int my_id;

int client_update_handler(struct msg_client_update *cu) {
	//DEBUGPRINT("client_update_handler: State is %d\n", state);
	if(state == STATE_LEADER_ELECTION) {
		if(cu->server_id != my_id) {
			//DEBUGPRINT("client_update_handler: not applicable\n", NULL);
			return 0;
		}
		if(enqueue_update(cu)) {
			//DEBUGPRINT("client_update_handler: adding to pending\n", NULL);
			add_to_pending_updates(cu);
			//DEBUGPRINT("client_update_handler: added to pending\n", NULL);
			return 0;
		}
	}
	
	if(state == STATE_REG_NONLEADER) {
	
		//DEBUGPRINT("ServerID: %d, MyId: %d\n",cu->server_id,my_id);
	
		if(cu->server_id == my_id) {
			//DEBUGPRINT("client_update_handler: adding to pending\n", NULL);
			add_to_pending_updates(cu);
			//DEBUGPRINT("client_update_handler: added to pending\n", NULL);
			return 1;
		}
	}
	
	if(state == STATE_REG_LEADER) {
	
		//DEBUGPRINT("Received Update %s\n",cu->update);
	
		if(enqueue_update(cu)) {
			if(cu->server_id == my_id) {
				add_to_pending_updates(cu);
			}
			//DEBUGPRINT("!!! *** !!! *** BEFORE FINISHING CUH\n",NULL);
			send_proposal();
			//DEBUGPRINT("!!! *** !!! *** Done With CUH\n",NULL);
			return 0;
		}	
	}
	
}
